package cn.zswltech.gruul.xxljob;

import cn.hutool.core.util.ObjectUtil;
import cn.zswltech.gruul.common.dal.dao.UserDOMapper;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.*;

/**
 * @author Jim
 * @version 1.0.0
 * @descripition: 用户内存队列
 * @date 2024/2/19 11:20
 */
@Component
@Slf4j
public class YunHuInfoQueue implements Runnable{
    @Autowired
    private YunHuUserMapper yunHuUserMapper;
    @Autowired
    private UserDOMapper userDOMapper;
    @Autowired
    private HttpYunHuInfoSync yunHuInfoSyncService;

    // 用户队列
    private final LinkedBlockingQueue<List<YunHuUser>> userBlockingQueue;
    // 机构队列
    private final LinkedBlockingQueue<List<YunHuOrg>> orgBlockingQueue;
    private final ExecutorService executorService;
    public YunHuInfoQueue() {
        this.userBlockingQueue = new LinkedBlockingQueue<>(1024);
        this.orgBlockingQueue = new LinkedBlockingQueue<>(1024);
        int coreSize = Runtime.getRuntime().availableProcessors()*2;
        this.executorService = new ThreadPoolExecutor(1, coreSize, 60000,
                TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>(),
                new ThreadFactoryBuilder().setNameFormat("user-syncer-%d").build(),new ThreadPoolExecutor.AbortPolicy());
    }
    // 队列为空标识
    private volatile Boolean queueEmpty = true;


    @Override
    public void run() {
        while (true){
            if (!queueEmpty){
                log.info("队列不为空开始同步数据" );
                List<YunHuUser> yunHuUserList = userBlockingQueue.poll();
                if (ObjectUtil.isNotEmpty(yunHuUserList)){
                    executorService.submit(new YunHuUserFeatureTask(yunHuUserList));
                }else {
                    List<YunHuOrg> yunHuOrgList = orgBlockingQueue.poll();
                    if (ObjectUtil.isNotEmpty(yunHuOrgList)){
                        executorService.submit(new YunHuOrgFeatureTask(yunHuOrgList));
                    }
                }
                refreshQueueEmptyStaus();
            }
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 刷新队列状态
     */
    private void refreshQueueEmptyStaus(){
        log.info("刷新队列状态");
        if (userBlockingQueue.isEmpty() && orgBlockingQueue.isEmpty()){
            queueEmpty = true;
            log.info("队列空");
        }else {
            queueEmpty = false;
            log.info("队列非空");
        }

    }



    /**
     * 云湖用户数据入队
     * @param list 云湖用户数据
     */
    public void offerUser(List<YunHuUser> list){
        if (ObjectUtil.isEmpty(list)){
            return;
        }
        userBlockingQueue.offer(list);
        refreshQueueEmptyStaus();
    }

    /**
     * 云湖机构数据入队
     * @param list 云湖机构数据
     */
    public void offerOrg(List<YunHuOrg> list){
        if (ObjectUtil.isEmpty(list)){
            return;
        }
        orgBlockingQueue.offer(list);
       refreshQueueEmptyStaus();
    }
}
